package com.cykj.v_m_sprintboot.controller;

import com.cykj.v_m_sprintboot.bean.TbSupplier;
import com.cykj.v_m_sprintboot.service.TbSupplierService;
import com.cykj.v_m_sprintboot.tool.GetTime;
import org.apache.ibatis.session.RowBounds;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TbSupplierController {
    @Autowired
    private TbSupplierService tbSupplierService;
    //添加供应商
    @RequestMapping("addSupplier")
    @ResponseBody
    public String addSupplier(@RequestBody TbSupplier tbSupplier){
        tbSupplier.setModifyTime(GetTime.getTime());
        tbSupplier.setSupplierTime(GetTime.getTime());

        tbSupplier.setSupplierType(StringUtils.join(tbSupplier.getGoodsType()));
       TbSupplier ts =tbSupplierService.selectSupplier(tbSupplier);
       if (ts==null){
           tbSupplierService.addSupplier(tbSupplier);
           return "添加成功";
       }
       return "供应商不能重名";
    }
    //获取供应商的列表
    @RequestMapping("getSupplierList")
    @ResponseBody
    public Map<String,Object> getSupplierList(@RequestBody TbSupplier tbSupplier){
        tbSupplier.setSupplierStateId(1);
        RowBounds row =new RowBounds((tbSupplier.getPage()-1)*tbSupplier.getSize(),tbSupplier.getSize());
        Map<String,Object> map=new HashMap<>();
       List<TbSupplier> supplierList=tbSupplierService.getSupplierList(tbSupplier,row);
        map.put("count",tbSupplierService.countSupplierList(tbSupplier));
        map.put("supplierList",supplierList);
        return map;
    }

    //修改供应商的信息
    @RequestMapping("updateSupplierInfo")
    @ResponseBody
    public String updateSupplierInfo(@RequestBody TbSupplier tbSupplier){
        tbSupplier.setModifyTime(GetTime.getTime());
        tbSupplier.setSupplierType(StringUtils.join(tbSupplier.getGoodsType()));
        TbSupplier ts =tbSupplierService.selectSupplier(tbSupplier);
        if (ts==null){
            tbSupplierService.updateSupplierInfo(tbSupplier);
            return "修改成功";
        }
        return "供应商不能重名";
    }

    //删除供应商
    @RequestMapping("delSupplier")
    @ResponseBody
    public String delSupplier(@RequestBody TbSupplier tbSupplier){
        tbSupplier.setSupplierStateId(2);
        tbSupplier.setModifyTime(GetTime.getTime());
        tbSupplierService.delSupplier(tbSupplier);
        return "移除成功";
    }
}
